package com.atguigu.edu.realtime.publisher.controller;

import com.atguigu.edu.realtime.publisher.bean.SourceTraPer;
import com.atguigu.edu.realtime.publisher.bean.TradeCt;
import com.atguigu.edu.realtime.publisher.service.TradeCountService;
import com.atguigu.edu.realtime.publisher.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TradeCountController {
    @Autowired
    TradeCountService tradeCountService;
    @RequestMapping("/source")
    public String getSourceOrderCnt(@RequestParam(value = "date", defaultValue = "1")Integer date){
        if(date == 1){
            date = DateUtil.now();
        }
        List<TradeCt> subjectOrderCnt = tradeCountService.getSourceOrderCnt(date);
        return "";
    }

    @RequestMapping("/province")
    public String getProvinceOrderCnt(@RequestParam(value = "date", defaultValue = "1")Integer date){
        if(date == 1){
            date = DateUtil.now();
        }
        List<TradeCt> courseOrderCnt = tradeCountService.getProvinceOrderCnt(date);
        StringBuilder sb = new StringBuilder("{\"status\": 0,\"msg\": \"\",\"data\": {\"mapData\": [");
        for (int i = 0; i< courseOrderCnt.size();i++){
            TradeCt tradeCt = courseOrderCnt.get(i);
            sb.append("{\n" +
                    "        \"name\": \""+ tradeCt.getName()+"\",\n" +
                    "        \"value\": "+ tradeCt.getUCt()+",\n" +
                    "        \"sizeValue\": "+ tradeCt.getOrderCt() +",\n" +
                    "        \"type\": "+ tradeCt.getOrderAmount() +"\n" +
                    "      }");
            if(i < courseOrderCnt.size() - 1){
                sb.append(",");
            }
        }
        sb.append(" ],\n" +
                "    \"valueName\": \"下单人数\",\n" +
                "     \"sizeValueName\": \"下单次数\",\n" +
                "    \"typeName\": \"下单金额\" \n" +
                "  }\n" +
                "}");
        return sb.toString();
    }

    @RequestMapping("/all")
    public String getOrderCnt(@RequestParam(value = "date", defaultValue = "1")Integer date){
        if(date == 1){
            date = DateUtil.now();
        }
        List<TradeCt> categoryOrderCnt = tradeCountService.getOrderCnt(date);
        return "";
    }
    @RequestMapping("/ordertraper")
    public String getSourceTraPer(@RequestParam(value = "date", defaultValue = "1")Integer date){
        if(date == 1){
            date = DateUtil.now();
        }
        List<SourceTraPer> sourceTraPer = tradeCountService.getSourceTraPer(date);
        return "";
    }
}
